package regions

import (
	"context"
	"product/common/datetime"
	"product/common/enumeration"

	"product/settings/serve/internal/svc"
	"product/settings/serve/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type QueryRegionLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// Query regions
func NewQueryRegionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryRegionLogic {
	return &QueryRegionLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *QueryRegionLogic) QueryRegion(req *types.QueryAreaListReq) (resp *types.QueryAreaListResp, err error) {
	list, err := l.svcCtx.RegionsModel.FindAllByPage(l.ctx, int64(req.Page), int64(req.PageSize))
	if err != nil {
		return &types.QueryAreaListResp{
			Code:    enumeration.ProcessingFailed,
			Message: err.Error(),
			Data:    []types.AreaInfo{},
		}, err
	}
	var data []types.AreaInfo
	for _, item := range list {
		data = append(data, types.AreaInfo{
			ID:        item.ID,
			Code:      item.Code,
			Name:      item.Name,
			Level:     int(item.Level),
			ParentId:  item.ParentId,
			CreatedAt: datetime.NewUnix(item.CreatedAt).ToFormatByTp(datetime.YearMouthDayHourMinute),
			UpdatedAt: datetime.NewUnix(item.UpdatedAt).ToFormatByTp(datetime.YearMouthDayHourMinute),
		})
	}
	return &types.QueryAreaListResp{
		Data:    data,
		Code:    enumeration.ProcessingSuccess,
		Message: "查询成功",
	}, nil

}
